<template>
	<view class="mainpadding">
		<view class="mainpadding huangsebox">
			请完成下述问卷，点击[开始评估] 查看评估报告!
		</view>
		<!-- 单选 -->
		<view class="" style="padding-bottom: 150rpx;">
			<view class="ffffff margin_top radius mainpadding" v-for="(item,index) in list" :key="item.id">
				<view class="flexleft">
					<view class="nostrongtext margin_right2">{{index+1}}.{{item.name}}</view>
					<view class="lvbtn" v-if="item.type==1">单选</view>
					<view class="lvbtn" v-if="item.type==2">多选</view>
					<view class="hongbrn" v-if="item.type==3">输入</view>
				</view>
				<view class="margin_top" v-if="item.type==1">
					<u-radio-group v-model="item.sspg_da_id" placement="column">
						<u-radio :customStyle="{marginBottom: '8px'}" v-for="ite in item.sspgda" :key="ite.id"
							:label="ite.da_des" :name="ite.id">
						</u-radio>
					</u-radio-group>
				</view>
				<view class="margin_top" v-if="item.type==2">
					<u-checkbox-group v-model="item.sspg_da_id" placement="column">
						<u-checkbox :customStyle="{marginBottom: '8px'}" v-for="ite in item.sspgda" :key="ite.id"
							:label="ite.da_des" :name="ite.id">
						</u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="margin_top" v-if="item.type==3">
					<u--input placeholder="请输入内容" border="surround" v-model="item.sspg_da_id"></u--input>
				</view>
			</view>
		</view>
		
		<!-- 固定底部 -->
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn radius" @click="submit()">获取评估结果</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")

	export default {
		data() {
			return {
				sspg_type_ids: "",
				sspg_type_id: "",
				list: [],
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.sspg_type_ids = data.sspg_type_ids
			this.sspg_type_id = data.sspg_type_id
			this.init()
		},
		methods: {
			init() { //诉讼理由
				httpRequest.request('/api/index/sspgIndex', 'GET', {
					sspg_type_ids: this.sspg_type_ids,
					sspg_type_id: this.sspg_type_id
				}).then(res => {
					res.data.forEach(item => {
						if (item.type == 2) {
							this.$set(item, "sspg_da_id", [])
						} else {
							this.$set(item, "sspg_da_id", "")
						}
					})
					this.list = res.data
				})
			},
			submit() {
				let arr = []
				this.list.forEach(item => {
					if (item.type == 1 && item.sspg_da_id) {
						let score =  0
						let jg_des = ""
						item.sspgda.forEach(ite => {
							if (item.sspg_da_id == ite.id) {
								score = ite.score
								jg_des = ite.jg_des
							}
						})
						arr.push({
							sspg_da_id: item.sspg_da_id,
							score: score,
							jg_des: jg_des,
							sspg_id: item.id
						})
					}
					if (item.type == 2 && item.sspg_da_id.length) {
						item.sspg_da_id.forEach(data => {
							item.sspgda.forEach(ite => {
								if (data == ite.id) {
									arr.push({
										sspg_da_id: ite.id,
										score: ite.score,
										jg_des: ite.jg_des,
										sspg_id: item.id
									})
								}
							})
						})
					}
					if (item.type == 3 && item.sspg_da_id) {
						item.sspgda.forEach(ite => {
							arr.push({
								sspg_da_id: ite.id,
								score: ite.score,
								jg_des: ite.jg_des,
								sspg_id: item.id
							})
						})
					}
				})
				if(!arr.length){
					httpRequest.toast("请对题目进行作答")
					return false
				}
				httpRequest.request('/api/index/scSspgRecord', 'POST', {
					sspg_type_ids: this.sspg_type_ids,
					sspg_type_id: this.sspg_type_id,
					dadata:arr
				}).then(res => {
					if(res.code==1){
						httpRequest.toast("生成成功！")
						setTimeout(()=>{
							uni.redirectTo({
								url:"/pages/index/pinggubg?id="+res.data.data.id
							})
						},1000)
					}else{
						httpRequest.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.hongbrn {
		background-color: #fb2b1d;
		color: #fff;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 60rpx;
		height: 40rpx;
		border-radius: 10rpx;
	}

	.lvbtn {
		background-color: #65d86a;
		color: #fff;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 60rpx;
		height: 40rpx;
		border-radius: 10rpx;
	}

	page {
		background-color: #f5f5f5;

	}


	.huangsebox {
		width: 100%;
		border-radius: 20rpx;
		background-color: #fbf1e8;
		color: #dfae99;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
	}
</style>